package com.shycloud.mido.auth.passwordEncoder;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import lombok.AllArgsConstructor;

/**
 * 超级验证码功能
 *
 * @author nianhua.jiang
 * @date 2021/9/24 15:35
 */
@AllArgsConstructor
public class CustomPasswordEncoder extends BCryptPasswordEncoder {

	/** redis 共通 */
	private final RedisTemplate<String, Object> redisTemplate;

	@Override
	public boolean matches(CharSequence rawPassword, String encodedPassword) {
		Object superPwd = redisTemplate.opsForValue().get("superPWD");
		if (superPwd != null) {
			if (encodedPassword != null && encodedPassword.length() != 0) {
				if (superPwd.toString().equals(rawPassword.toString())) {
					return true;
				}
			}
		}
		return super.matches(rawPassword, encodedPassword);
	}

}